<?php
/*
 * Copyright 2008 by Oliver Radwan, Maxwell Palmer, Nolan McNair,
* Taylor Talmage, and Allen Tucker.  This program is part of RMH Homebase.
* RMH Homebase is free software.  It comes with absolutely no warranty.
* You can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation
* (see <http://www.gnu.org/licenses/ for more information).
*/

/*
 * class Shift characterizes a time interval in a day
* for scheduling volunteers
* @version May 1, 2008, modified 9/15/08, 2/14/10
* @author Allen Tucker and Maxwell Palmer
*/
class Shift {
	private $mm_dd_yy;      // String: "mm-dd-yy".
	private $name;          // String: '9-12', '12-15', '15-18', '9-12Fam', '12-15Fam', '15-18Fam',
	// '18-21', '10-13', '13-16', '12-14', '14-17', 'night', or 'chef'
	private $start_time;     // Integer: e.g. 11 (meaning 11:00am)
	private $end_time;       // Integer: e.g. 13 (meaning 1:00pm)
	private $venue;        // String "" (for House) or "Fam" (for Family Room)
	private $vacancies;         // number of vacancies in this shift
	private $persons;       // array of person ids filling slots, followed by their name, ie "malcom1234567890+Malcom+Jones"
	private $sub_call_list; // "yes" or "no" for if SCL exists
	private $day;	// string name of day "Monday"...
	private $id;            // "mm-dd-yy-ss-ss" or "mm-dd-yy-ss-ssFam" or "mm-dd-yy-chef" is a unique key for this shift
	// the suffix "Fam" designates a shift on the family room calendar
	// the suffix "Chef" designates a guest chef on a particular date
	private $notes;		// notes written by the manager
	/*
	* construct an empty shift with a certain number of vacancies
	*/
	function __construct ($id, $vacancies,$persons,$sub_call_list,$notes) {
		$this->mm_dd_yy=substr($id,0,8);
		$this->vacancies=$vacancies;
		$this->id = $id;
		$this->name=substr($id, 9);
		$i = strpos(substr($id, 9), "-");  // non-chef and non-night shifts
		if ($i!==false) {
			$this->start_time = substr($id, 9, $i);
			$this->end_time = substr($id, 9+$i+1);
		}
		$i = strpos($id, "Fam");
		if ($i!==false)
			$this->venue = "Fam";
		else
			$this->venue = "";
		$this->day = date("D", mktime(0, 0, 0, substr($this->mm_dd_yy,0,2),
				substr($this->mm_dd_yy,3,2), "20".substr($this->mm_dd_yy,6,2)));
		if ($persons==null) {
			$this->persons=array();
			$this->notes="";
			$this->sub_call_list = "no";
		}
		else {
			$this->persons = $persons;
			$this->sub_call_list = $sub_call_list;
			$this->notes=$notes;
		}
	}
	/**
	 * This function (re)sets the start and end times for a shift
	 * and corrects its $id accordingly
	 * Precondition:  0 <= $st && $st < $et && $et < 24
	 *          && the shift is not "chef" or "night"
	 * Postcondition: $this->start_time == $st && $this->end_time == $et
	 *          && $this->id == $this->mm_dd_yy .  "-"
	 *          . $this->start_time . "-" . $this->end_time . $this->venue
	 *          && $this->name == substr($this->id, 9)
	 */
	function set_start_end_time ($st, $et) {
		if (0 <= $st && $st < $et && $et < 24 &&
				strpos(substr($this->id, 9), "-")!==false) {
			$this->start_time = $st;
			$this->end_time = $et;
			$this->id = $this->mm_dd_yy .  "-"  . $this->start_time
			. "-" . $this->end_time . $this->venue;
			$this->name=substr($this->id, 9);
			return $this;
		}
		else return false;
	}

	/*
	 * @return the number of vacancies in this shift.
	*/
	function num_vacancies() {
		return $this->vacancies;
	}
	function ignore_vacancy() {
		if ($this->vacancies>0)
			--$this->vacancies;
	}
	function add_vacancy() {
		++$this->vacancies;
	}
	function num_slots() {
		if(!$this->persons[0])
			array_shift($this->persons);
		return $this->vacancies + count($this->persons);
	}
	function has_sub_call_list () {
		if($this->sub_call_list=="yes")
			return true;
		return false;
	}
	function open_sub_call_list() {
		$this->sub_call_list="yes";
	}
	function close_sub_call_list() {
		$this->sub_call_list="no";
	}
	/*
	 * getters and setters
	*/
	function get_name() {
		return $this->name;
	}
	function get_start_time() {
		return $this->start_time;
	}
	function get_end_time() {
		return $this->end_time;
	}
	function get_date() {
		return $this->mm_dd_yy;
	}
	function get_venue() {
		return $this->venue;
	}
	function get_persons() {
		return $this->persons;
	}
	function get_sub_call_list() {
		return $this->sub_call_list;
	}
	function get_id() {
		return $this->id;
	}
	function get_day() {
		return $this->day;
	}
	function get_notes() {
		return $this->notes;
	}
	function set_notes($notes) {
		$this->notes=$notes;
	}
	function assign_persons($p) {
		$this->persons=$p;
	}
}
?>
